package com.helpers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.model.UploadedFile;

import com.databeans.Document;

public class DirectoryObserver {

	private static File rootDir=new File("D:\\PrimeDropBox");

	public DirectoryObserver(){
		
	}
	
	public static TreeNode getTree(){
		TreeNode root = new DefaultTreeNode("root", null); 
		
		TreeNode foldernode = new DefaultTreeNode(new Document("Root", "Folder","-",rootDir), root); 
			
		
		if(rootDir.exists())
			observeFile(foldernode,rootDir);

		return root;
	}
	
	
	
	public static void observeFile(TreeNode parentNode,File parentFile){
		File[] files = parentFile.listFiles();
	    for (File file : files) {
	    	if(file.isDirectory()){
				TreeNode foldernode = new DefaultTreeNode(new Document(file.getName(), "Folder","-",file), parentNode); 
				observeFile(foldernode,file);
			}
			else{
				new DefaultTreeNode("document", new Document(file.getName(), "File",getFileSize(file),file), parentNode);  
			}
	    }
	}
	
	
	public static String getFileSize(File f){
		if(f!=null && f.exists() && !f.isDirectory()){
			 
			double bytes = f.length();
			double kilobytes = (bytes / 1024);
			double megabytes = (kilobytes / 1024);
			double gigabytes = (megabytes / 1024);
			
			DecimalFormat df = new DecimalFormat("#.##");
			
			String ret="";
			
			ret=df.format(bytes)+" B";
			if(bytes>900){
				ret=df.format(kilobytes)+" KB";
			}
			if(kilobytes>900){
				ret=df.format(megabytes)+" MB";
			}
			if(megabytes>900){
				ret=df.format(gigabytes)+" GB";
			}
			
			return ret;
		}
		
		return "";
	}
	
	
	public static boolean deleteAll(File path) {
	    if( path.exists() ) {
	      File[] files = path.listFiles();
	      if(files!=null){
		      for(int i=0; i<files.length; i++) {
		         if(files[i].isDirectory()) {
		        	 deleteAll(files[i]);
		         }
		         else {
		           files[i].delete();
		         }
		      }
	      }
	    }
	    return( path.delete() );
	  }
	
	
	public static File copyFileToFolder(UploadedFile file, InputStream in,File folder) throws Exception {
		File newFile=null;
		
		String fileName = file.getFileName();
		String destination = folder.getAbsolutePath();
		String path = destination + "\\" + fileName;

		try {
			// write the inputStream to a FileOutputStream
			newFile = new File(path);
			OutputStream out = new FileOutputStream(newFile);

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}

			in.close();
			out.flush();
			out.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		
		return newFile;
	}
	
}
